﻿using Entity;
using IRepository;
using IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
    public class OutboundManagementService : IOutboundManagementService
    {

        private readonly IOutboundManagementRepository _iou;
        public OutboundManagementService(IOutboundManagementRepository iou) => _iou = iou;

        public Task<int> AddOutboundOrderAsync(WMSKNZYDeliveryNote order, List<WMSKNZYOutboundDetails> details)
        {
           return _iou.AddOutboundOrderAsync(order, details);
        }

        public Task AuditAsync(OutboundAuditDTO dto)
        {
          return _iou.AuditAsync(dto);
        }

        public async Task<PagedViewModel<WMSOrderSalesOrder3>> GetAllWMSOutboundAsync(string sales_order_no = null, string document_status = null, string customer_name = null, DateTime? sales_date = null, string sales_person = null,
string sales_dept = null,int currentPage = 1, int pageSize = 2)
        {
            // 1. 查询符合条件的全量数据
            List<WMSOrderSalesOrder3> wMS_Basic_Customer2s = await _iou.GetAllWMSOutboundAsync(sales_order_no, document_status, customer_name, sales_date,sales_person,sales_dept);

            // 2. 内存分页（Skip + Take）
            List<WMSOrderSalesOrder3> wMS_Basic_Customers = wMS_Basic_Customer2s
                .Skip((currentPage - 1) * pageSize)  // 跳过前N条
                .Take(pageSize)                       // 取当前页数据
                .ToList();

            // 3. 封装分页模型（包含“当前页数据”和“总条数”）
            PagedViewModel<WMSOrderSalesOrder3> result = new PagedViewModel<WMSOrderSalesOrder3>
            {
                items = wMS_Basic_Customers,  // 修正：与PagedViewModel的属性名保持一致（建议用items）
                total = wMS_Basic_Customer2s.Count    // 总数据条数
            };

            return result;
        }

        public Task<int> GetByLocationAndProduct(int locationId)
        {
           return _iou.GetByLocationAndProduct(locationId);
        }

        public Task<List<StorageLocationEntity2>> GetByWarehouseId(int warehouseId)
        {
          return _iou.GetByWarehouseId(warehouseId);
        }

        public Task<int> GetInventoryByTargetSql(int warehouseId, int locationId, string productCode)
        {
           return _iou.GetInventoryByTargetSql(warehouseId, locationId, productCode);
        }

        public Task<List<WMSOrderSalesOrder4>> GetOutboundById(string salesOrderNo)
        {
            return _iou.GetOutboundById(salesOrderNo);
        }

        public async Task<PagedViewModel<OutboundOrderDTO>> GetOutboundOrders(string outboundOrderNo = null, string outboundType = null, string outboundStatus = null, string warehouseName = null, int currentPage = 1, int pageSize = 2)
        {
            // 1. 查询符合条件的全量数据
            List<OutboundOrderDTO> wMS_Basic_Customer2s = await _iou.GetOutboundOrders(outboundOrderNo, outboundType, outboundStatus, warehouseName);

            // 2. 内存分页（Skip + Take）
            List<OutboundOrderDTO> wMS_Basic_Customers = wMS_Basic_Customer2s
                .Skip((currentPage - 1) * pageSize)  // 跳过前N条
                .Take(pageSize)                       // 取当前页数据
                .ToList();

            // 3. 封装分页模型（包含“当前页数据”和“总条数”）
            PagedViewModel<OutboundOrderDTO> result = new PagedViewModel<OutboundOrderDTO>
            {
                items = wMS_Basic_Customers,  // 修正：与PagedViewModel的属性名保持一致（建议用items）
                total = wMS_Basic_Customer2s.Count    // 总数据条数
            };

            return result;
        }

    }
}
